﻿using Ext.Net;
using NOAH.CMS.Cache;
using NOAH.CMS.Repository;

using System;
using System.Web.Profile;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.HtmlControls;


public partial class admin_Modules_NewsEvent_Links_AddOrEdit : System.Web.UI.Page
{
    private string id = string.Empty;

    public void AddOrEdit(object sender, EventArgs e)
    {
        try
        {
            using (LinkRepository repository = new LinkRepository())
            {
                Link entity = null;
                if (this.id != "0")
                {
                    entity = repository.Get(int.Parse(this.id));
                }
                else
                {
                    entity = new Link();
                }
                entity.IsPublish = new bool?(this.chkPublish.Checked);
                entity.ImageUrl = this.triggerAttachImage.Text;
                entity.Name = this.txtTitle.Text;
                entity.Url = this.txtUrl.Text;
                int temp = 0;
                if (this.txtImageDetailOrder.Value != null)
                {
                    int.TryParse(this.txtImageDetailOrder.Value.ToString(), out temp);
                    entity.Order = new short?((short)temp);
                }
                if (this.id == "0")
                {
                    repository.Add(entity);
                }
                repository.SubmitChanges();
                new LinkCacheLayer().InvalidateCache();
                ExtNet.Msg.Hide();
                Util.ShowMessage(Constants.UPDATE_SUCCESS);
                BasePage.RegisterCallBackFromChild(this.ScriptManager2, string.Empty);
            }
        }
        catch
        {
            Util.ShowMessage2(Constants.UPDATE_FAILURE);
        }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        id = base.Request.QueryString["id"] ?? "0";

        this.txtTitle.Focus(false, 100);
        if (!ExtNet.IsAjaxRequest)
        {
            this.ShowDetail();
        }
    }

    private void ShowDetail()
    {
        if (this.id != "0")
        {
            using (LinkRepository contactRepositiory = new LinkRepository())
            {
                Link entity = contactRepositiory.Get(int.Parse(this.id));
                this.txtTitle.Text = entity.Name;
                short? temp = entity.Order;
                this.txtImageDetailOrder.Text = (temp.HasValue ? ((int)temp.GetValueOrDefault()) : 0).ToString();
                this.txtUrl.Text = entity.Url;
                this.triggerAttachImage.Text = entity.ImageUrl;
                bool? temp1 = entity.IsPublish;
                this.chkPublish.Checked = temp1.HasValue ? temp1.GetValueOrDefault() : false;
            }
        }
    }

}